#!/usr/bin/env python3
"""
通用打包脚本
支持 macOS 和 Windows 平台打包
"""

import os
import sys
import platform
import subprocess

def check_pyinstaller():
    """检查是否安装了 pyinstaller"""
    try:
        import PyInstaller
        print("✓ PyInstaller 已安装")
        return True
    except ImportError:
        print("❌ PyInstaller 未安装，正在安装...")
        try:
            subprocess.check_call([sys.executable, "-m", "pip", "install", "pyinstaller"])
            print("✓ PyInstaller 安装成功")
            return True
        except subprocess.CalledProcessError:
            print("❌ PyInstaller 安装失败")
            return False

def build_for_platform():
    """根据当前平台构建可执行文件"""
    current_platform = platform.system()
    
    if current_platform == "Darwin":  # macOS
        return build_macos()
    elif current_platform == "Windows":
        return build_windows()
    else:
        print(f"❌ 不支持的操作系统: {current_platform}")
        return False

def build_macos():
    """构建 macOS 可执行文件"""
    print("开始构建 macOS 可执行文件...")
    
    cmd = [
        "pyinstaller",
        "--onefile",
        "--windowed",
        "--name=Telegram群发软件",
        "--hidden-import=tkinter",
        "--hidden-import=tkinter.ttk",
        "--hidden-import=tkinter.messagebox",
        "--hidden-import=tkinter.filedialog",
        "--hidden-import=tkinter.scrolledtext",
        "--hidden-import=tkinter.simpledialog",
        "--hidden-import=telethon",
        "--hidden-import=asyncio",
        "--hidden-import=threading",
        "--hidden-import=queue",
        "--hidden-import=json",
        "--hidden-import=datetime",
        "--hidden-import=os",
        "--hidden-import=sys",
        "telegram_mass_sender.py"
    ]
    
    try:
        subprocess.check_call(cmd)
        print("✓ macOS 构建成功！")
        return True
    except subprocess.CalledProcessError as e:
        print(f"❌ 构建失败: {e}")
        return False

def build_windows():
    """构建 Windows 可执行文件"""
    print("开始构建 Windows 可执行文件...")
    
    cmd = [
        "pyinstaller",
        "--onefile",
        "--windowed",
        "--name=Telegram群发软件",
        "--hidden-import=tkinter",
        "--hidden-import=tkinter.ttk",
        "--hidden-import=tkinter.messagebox",
        "--hidden-import=tkinter.filedialog",
        "--hidden-import=tkinter.scrolledtext",
        "--hidden-import=tkinter.simpledialog",
        "--hidden-import=telethon",
        "--hidden-import=asyncio",
        "--hidden-import=threading",
        "--hidden-import=queue",
        "--hidden-import=json",
        "--hidden-import=datetime",
        "--hidden-import=os",
        "--hidden-import=sys",
        "telegram_mass_sender.py"
    ]
    
    try:
        subprocess.check_call(cmd)
        print("✓ Windows 构建成功！")
        return True
    except subprocess.CalledProcessError as e:
        print(f"❌ 构建失败: {e}")
        return False

def create_package_info():
    """创建打包信息文件"""
    info_content = f"""# Telegram 群发软件 - 打包信息

## 打包时间
{platform.system()} {platform.release()}

## 系统信息
- 操作系统: {platform.system()}
- 系统版本: {platform.release()}
- Python版本: {sys.version}

## 生成文件
- 可执行文件: dist/Telegram群发软件
- 配置文件: telegram_config.json (首次运行时生成)

## 使用说明
1. 双击运行可执行文件
2. 首次使用需要配置 Telegram API
3. 按照界面提示操作

## 注意事项
- 请遵守 Telegram 使用条款
- 避免频繁发送消息，防止账号被封
- 首次使用需要网络连接
"""
    
    with open("打包信息.txt", "w", encoding="utf-8") as f:
        f.write(info_content)
    
    print("✓ 已生成打包信息文件")

if __name__ == "__main__":
    print("=" * 50)
    print("Telegram 群发软件 - 通用打包工具")
    print("=" * 50)
    print(f"当前平台: {platform.system()} {platform.release()}")
    print("=" * 50)
    
    if not check_pyinstaller():
        sys.exit(1)
    
    if build_for_platform():
        create_package_info()
        print("\n🎉 打包完成！")
        print("可执行文件位置: dist/Telegram群发软件")
        print("打包信息: 打包信息.txt")
    else:
        print("\n❌ 打包失败！")
        sys.exit(1) 